﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.UI.Xaml.Controls;
using Win8NearMe.Bean;

namespace Win8NearMe
{
    class CParseUtil
    {
        public static async Task<List<Person>> parseFriends(String data)
        {
            List<Person> arrFriends = new List<Person>();
            JsonObject root = JsonObject.Parse(data);
            double userId = root.GetNamedNumber("userID");
            JsonArray array = root.GetNamedArray("friends");
            
            uint i=0;
            int n = array.Count;
            for (i = 0; i < n; i++)
            {
               JsonObject obj = array.GetObjectAt(i);
               double id = obj.GetNamedNumber("id");
               String name = obj.GetNamedString("name");
               String status = obj.GetNamedString("status");
               String lon = obj.GetNamedString("long");
               String lat = obj.GetNamedString("lat");
               CDistance distance = new CDistance();
               JsonObject objDis = obj.GetNamedObject("distance");
               double miles = objDis.GetNamedNumber("miles");
               double yards = objDis.GetNamedNumber("yards");
               double kilometers = objDis.GetNamedNumber("kilometers");
               double meters = objDis.GetNamedNumber("meters");
               distance.miles = miles;
               distance.yards = yards;
               distance.kilometers = kilometers;
               distance.meters = meters;
               arrFriends.Add(new Person(id,name,status, lat, lon, distance));
            }
        
            return arrFriends;
        }

    }
}
